package com.demo.controller;

import cn.hutool.crypto.digest.MD5;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.demo.dto.AlarmPolicyDto;
import com.demo.dto.WorkOrderDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

/**
 *
 */
@RestController
@RequestMapping("/smartFireApiTest")
public class SmartFireApiTestController {

    private final Logger logger = LoggerFactory.getLogger(SmartFireApiTestController.class);

    //    private final String baseUrl = "https://tyzhxf-api.ctwing.cn";
//    private final String appId = "gxchengyun";
//    private final String userId = "50161";
//    private final String appKey = "A5FWIAlvENT5D3q2";
    private final String baseUrl = "http://localhost:8806";
    private final String appId = "test";
    private final String userId = "20";
    private final String appKey = "test@111";

    @GetMapping("/api/v1/auth/getAccessToken")
    public JSONObject getAccessToken() {
        HashMap<String, String> headerMap = new HashMap<>();
        headerMap.put("appId", appId);
        headerMap.put("userId", userId);
        long timestamp = System.currentTimeMillis();
        String sign = MD5.create().digestHex(userId + timestamp + appKey);
        String thisUrl = baseUrl + "/api/v1/auth/getAccessToken" + "?timestamp=" + timestamp + "&sign=" + sign;
        HttpResponse response = HttpUtil.createGet(thisUrl).headerMap(headerMap, false).execute();
        logger.info(response.toString());
        return JSONUtil.parseObj(response.body());
    }

    private String getAccessTokenStr() {
        JSONObject jsonObject = this.getAccessToken();
        JSONObject data = jsonObject.get("data", JSONObject.class);
        return data.get("accessToken", String.class);
    }

    private Map<String, String> getHeaderMap() {
        // 构造请求头
        HashMap<String, String> headerMap = new HashMap<>();
        headerMap.put("accessToken", this.getAccessTokenStr());
        headerMap.put("appId", appId);
        headerMap.put("userId", userId);
        return headerMap;
    }

    @GetMapping("/api/v1/screen/user/userInfo")
    public String userInfo() {
        Map<String, String> headerMap = this.getHeaderMap();
        String thisUrl = baseUrl + "/api/v1/screen/user/userInfo";
        HttpResponse response = HttpUtil.createGet(thisUrl).headerMap(headerMap, false).execute();
        logger.info(response.toString());
        return JSONUtil.formatJsonStr(response.body());
    }

    @GetMapping("/api/v1/app/subscription/create")
    public String createSubscription() {
        // 构造请求头
        Map<String, String> headerMap = this.getHeaderMap();
        // 请求路径
        String thisUrl = baseUrl + "/api/v1/app/subscription/create";
        // 构造body
        JSONObject bodyJsonObject = JSONUtil.createObj();
        HashSet<String> scopes = new HashSet<>();
        scopes.add("ALARM");
        bodyJsonObject
                .put("appId", appId)
                .put("scopes", scopes)
                .put("url", "http://gl.fudacoldchain2.cn/api/dorrsenor/dorrsenor/reception");

        // 发出请求
        HttpRequest request = HttpUtil.createPost(thisUrl)
                .headerMap(headerMap, false)
                .body(bodyJsonObject.toJSONString(2));
        logger.warn(request.toString());
        HttpResponse response = request.execute();
        logger.warn(response.toString());
        return JSONUtil.formatJsonStr(response.body());
    }

    @GetMapping("/api/v1/app/subscription/delete")
    public String delSubscription() {
        // 构造请求头
        Map<String, String> headerMap = this.getHeaderMap();
        // 请求路径
        String thisUrl = baseUrl + "/api/v1/app/subscription/delete";


        // 发出请求
        HttpRequest request = HttpUtil.createGet(thisUrl)
                .headerMap(headerMap, false);
        logger.warn(request.toString());
        HttpResponse response = request.execute();
        logger.warn(response.toString());
        return JSONUtil.formatJsonStr(response.body());
    }

    // 巡检工单回单
    @PostMapping("/api/v1/workorder/uploadResult")
    public String uploadResult(@RequestBody WorkOrderDto workOrderDto) {
        // 构造请求头
        Map<String, String> headerMap = this.getHeaderMap();
        // 请求路径
        String thisUrl = baseUrl + "/api/v1/workorder/uploadResult";
        // 构造body
        JSONObject bodyJsonObject = JSONUtil.createObj();
        bodyJsonObject.put("workOrderId", workOrderDto.getWorkOrderId());

        // 发出请求
        HttpRequest request = HttpUtil.createPost(thisUrl)
                .headerMap(headerMap, false)
                .body(bodyJsonObject.toJSONString(2));
        logger.info(request.toString());
        HttpResponse response = request.execute();
        logger.info(response.toString());
        return JSONUtil.formatJsonStr(response.body());
    }

    // 查询告警策略
    @PostMapping("/api/v1/device/alarmPolicy/qryAlarmPolicyPageList")
    public String qryAlarmPolicyPageList() {
        // 构造请求头
        Map<String, String> headerMap = this.getHeaderMap();
        // 请求路径
        String thisUrl = baseUrl + "/api/v1/device/alarmPolicy/qryAlarmPolicyPageList";
        // 构造body
        JSONObject bodyJsonObject = JSONUtil.createObj();
        bodyJsonObject.put("categoryId", 1).put("depId", 1000);

        // 发出请求
        HttpRequest request = HttpUtil.createPost(thisUrl)
                .headerMap(headerMap, false)
                .body(bodyJsonObject.toJSONString(2));
        logger.warn(request.toString());
        HttpResponse response = request.execute();
        logger.warn(response.toString());
        return JSONUtil.formatJsonStr(response.body());
    }

    // 查询告警策略
    @PostMapping("/api/v1/device/alarmPolicy/ifGenAlarmPolicyWork")
    public String changeIfGenAlarmPolicyWork(@RequestBody AlarmPolicyDto alarmPolicy) {
        // 构造请求头
        Map<String, String> headerMap = this.getHeaderMap();
        // 请求路径
        String thisUrl = baseUrl + "/api/v1/device/alarmPolicy/ifGenAlarmPolicyWork";
        // 构造body
        JSONObject bodyJsonObject = JSONUtil.parseObj(alarmPolicy);

        // 发出请求
        HttpRequest request = HttpUtil.createPost(thisUrl)
                .headerMap(headerMap, false)
                .body(bodyJsonObject.toJSONString(2));
        logger.warn(request.toString());
        HttpResponse response = request.execute();
        logger.warn(response.toString());
        return JSONUtil.formatJsonStr(response.body());
    }
}
